import {Component} from '@angular/core';
import {NavController, NavParams, ViewController, ToastController, LoadingController} from 'ionic-angular';
import {UserInfo} from "../../model/UserInfo";
import {SportModel} from "../../model/SportModel";
import {PubMoreInfoPage} from "../pub-more-info/pub-more-info";
import {StorageService} from "../../providers/StorageService";

import {STATUS_SUCCESS, USERFACE_PATH} from "../../providers/Constants";
import {SportService} from "../../providers/SportService";

declare var AMap;
var place;  //定义一个第三方变量传值给sportModel.pubSportPlace

@Component({
  selector: 'page-publish-sport',
  templateUrl: 'publish-sport.html',
})
export class PublishSport {

  // startTime: String ;//获取当前系统时间,也是开始时间(数据双向绑定)
  // endTime:String;//设置结束时间
  min: string;//最小时间(为当前时间，注意一个问题，Angular是双向绑定数据，所以不能直接等于now)
  max: string;//最大时间(为now+7天)
  // isModifyTel:false;

  user: UserInfo;
  userFaceImg: String;
  public map: any;
  public searchPlace: string;
  a: any;
  sportTypeImg: string;
  info: any;
  nickName:string;
  // pubSport: FormGroup;
  // // startTime: any;
  // // endTime: any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public  viewCtrl: ViewController,
              public storageService: StorageService,
              public sportModel: SportModel,
              public sportService: SportService,
              public toastCtrl: ToastController,
              public loadingCtrl: LoadingController) {

    //条用高德地图服务


  }

  ionViewDidLoad() {
    console.log('发布运动');
      this.initSportInfo();


    //地图加载
    var map = new AMap.Map("container", {
      resizeEnable: true
    });
    var autoOptions = {
      input: "tipinput"
    };
    // console.log(autoOptions);
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
      map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
      placeSearch.setCity(e.poi.adcode);
      placeSearch.search(e.poi.name);  //关键字查询查询

      place = e.poi.name;
    }

  }

  // 时间标准化
  calculateTime(offset: any) {
    let d = new Date();
    let nd = new Date(d.getTime() + (3600000 * offset));
    // return nd;
    return nd.toISOString();
  }

  ionViewWillEnter() {


  }

  // 关闭当前页面
  dismiss() {
    this.viewCtrl.dismiss();
  }

  //发布活动
  pubSport() {
    this.readCaech();
    console.log(JSON.stringify(this.sportModel));
    this.sportModel.pubSportPlace =place;
    let loader = this.loadingCtrl.create({
      content: "正在创建...",
    });
    loader.present();
    setTimeout(() => {
      this.sportService.pubSport(this.sportModel).then(res => {
        if(res["status"]==STATUS_SUCCESS){
          this.toastCtrl.create({message:res["msg"], duration: 4500}).present();
          loader.dismiss();
          this.deleteCaech();//删除缓存数据
          this.viewCtrl.dismiss();
          // this.navCtrl.push(SportPage);
          return;
        }
        loader.dismiss();
        this.toastCtrl.create(
          {message:res["msg"], duration: 4500}).present();
      });

    }, 2000);


  }

  //初始化数据
  test() {
    if (this.a == "show") {
      this.a = "hidden";
    } else {
      this.a = "show";
    }
  }

  selectSprotType( sportTypeId,sportType) {
    this.sportTypeImg = sportType;
    this.sportModel.pubSportTypeId = sportTypeId;
    this.a = "hidden";
  }

  pushPage(type) {
    this.sportModel.pubSportPlace =place;
    // this.storageService.write('SportModel', this.sportModel);
    this.navCtrl.push(PubMoreInfoPage, {"dataType": type});


  }

  //页面初始化信息
  initSportInfo(){
    this.deleteCaech();
    this.sportModel.pubSportTitle=null;
    this.sportModel.pubSportDes=null;
    this.sportModel.phone = null;
    this.sportModel.sportAllowJoinPersonNum=null;

    console.log(JSON.stringify(this.sportModel));
    this.sportModel.sportStartDatetime = this.calculateTime('+8');
    this.sportModel.sportEndDatetime = this.calculateTime('+8');
    this.min = this.calculateTime('+8');
    this.max = this.calculateTime('+176');//一周为168小时+8校准时间


    this.user = this.storageService.read<UserInfo>('UserInfo');
    this.userFaceImg = USERFACE_PATH + "/" + this.user.userFace;
    //初始化化
    this.nickName =this.user.nickname
    this.sportTypeImg = "run";
    this.sportModel.pubSportTypeId=1;
    // this.sportModel.pubSportTitle = this.user.nickname + "邀你一起";
    this.sportModel.pubUserId = this.user.id;
  }
  //删除缓存数据
  deleteCaech(){
    if(this.storageService.read("pubSportDes")!=null){
      this.storageService.remove("pubSportDes");
    }
    if(this.storageService.read("sportAllowJoinPersonNum")!=null){
      this.storageService.remove("sportAllowJoinPersonNum");
    }
    if(this.storageService.read("phone")!=null){
      this.storageService.remove("phone");
    }

  }
  //读取缓存数据
  readCaech(){
    if(this.storageService.read("pubSportDes")!=null&&this.storageService.read("pubSportDes")["pubSportDes"]!=''){
      // var dathis.storageService.read("pubSportDes")
      this.sportModel.pubSportDes = this.storageService.read("pubSportDes")["pubSportDes"];
    }
    if(this.storageService.read("sportAllowJoinPersonNum")!=null
    &&this.storageService.read("sportAllowJoinPersonNum")["sportAllowJoinPersonNum"]!=''){
      this.sportModel.sportAllowJoinPersonNum = this.storageService.read("sportAllowJoinPersonNum")["sportAllowJoinPersonNum"];
    }
    if(this.storageService.read("phone")!=null
    &&this.storageService.read("phone")["phone"]!=''){
      this.sportModel.phone = this.storageService.read("phone")["phone"];
    }
  }

}

